package com.lancoo.znbkxx.teacher.ui.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.ComponentActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ClassCognitiveDiagnosisInfor
import com.lancoo.znbkxx.teacher.utils.ButtonUtils
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import kotlinx.android.synthetic.main.aibk_adapter_item_cognitive_diagnosis.view.*
import kotlinx.android.synthetic.main.aibk_adapter_item_cognitive_diagnosis.view.TvPeopleNum

class AibkCognitiveDiagnosisAdapter(
        val activity: ComponentActivity?,
        val classCognitiveDiagnosisInfor: ClassCognitiveDiagnosisInfor,
        val maxDistreNum:Int
): KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_item_cognitive_diagnosis, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {

        val distreNum = classCognitiveDiagnosisInfor.ClassIndexMap[position].DistreNum
        val distreIndex = classCognitiveDiagnosisInfor.ClassIndexMap[position].DistreIndex
        val distreIndexGradeName = classCognitiveDiagnosisInfor.ClassIndexMap[position].DistreIndexGradeName

        holder.convertView.TvPeopleNum.text = distreNum.toString() +"人"
        holder.convertView.IvCognitivePeopleNum.setImageResource(R.drawable.aibk_chart_background2_radius_blue_5)
        showIvData(holder.convertView.IvCognitivePeopleNum,distreNum, holder.convertView.TvCognitiveRange,distreIndex)
        holder.convertView.TvCognitiveRangeName.text = distreIndexGradeName

        holder.convertView.IvCognitivePeopleNum.setOnClickListener {
            //显示学生预估分数弹窗
            if (!ButtonUtils.isFastDoubleClick(R.id.IvGradePeopleNum)) {
                clickListener!!.onClick(position)
            }
        }
    }

    override fun getItemCount(): Int {
        return classCognitiveDiagnosisInfor.ClassIndexMap.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    private fun showIvData(Iv: ImageView, data:Int,
                           Tv: TextView, typeName:String){

        val para1 = Iv.layoutParams
        para1.height = DensityUtils.dp2px(activity,(data*200f)/maxDistreNum)
        Iv.layoutParams = para1

        Tv.text = typeName
    }

    interface OnClickListener {
        fun onClick(pos:Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickListener(clickListener: OnClickListener?) {
        this.clickListener = clickListener
    }

}